<template>
  <el-dialog
    :title="!material.materialId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="material" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="物资编号" prop="materialNum">
      <el-input v-model="material.materialNum" placeholder="物资编号" :readonly="!material.materialId ? false : true"></el-input>
    </el-form-item>
      <el-form-item label="物资名称" prop="materialName">
        <el-input v-model="material.materialName" placeholder="产品名称" @input="change"></el-input>
      </el-form-item>
      <el-form-item label="物资类别" prop="materialType">
        <el-select v-model="material.materialType" placeholder="请选择物资类别">
          <el-option  v-for="data in dataList" :label="data.desc" :value="data.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="价格" prop="materialPrice">
        <el-input v-model="material.materialPrice" placeholder="价格" type="number" :min="0"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="materialUnit">
        <el-input v-model="material.materialUnit" placeholder="单位"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="materialDesc">
        <el-input v-model="material.materialDesc" placeholder="描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        material: {
          materialId:'',
          materialNum:'',
          materialName:'',
          materialType:'',
          materialPrice:'',
          materialUnit:'',
          materialDesc:''
        },
        // 类别列表
        dataList: [],
        dataRule: {
          materialNum: [
            // required:非空验证
            { required: true, message: '物资编号不能为空', trigger: 'blur' }
          ],
          materialName: [
            { required: true, message: '物资名称不能为空', trigger: 'blur' },
            // { type: String, message: '请输入字符串', trigger: 'blur' }
          ],
          materialPrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' },
          ],
          materialUnit: [
            { required: true, message: '单位不能为空', trigger: 'blur' },
          ],
          materialDesc: [
            { required: true, message: '描述不能为空', trigger: 'blur' },
          ],
          materialType: [
            { required: true, message: '类别不能为空', trigger: 'blur' },
          ],
        }
      }
    },
    mounted(){
      this.getDataList()
    },
    methods: {
      change(){
        this.$forceUpdate();
      },
      // 获取类别数据列表
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/purchase/productCategory/materialType'),
          method: 'get',
        }).then((data) => {
          if (data && data.code === 200) {
            this.dataList = data.page.list
          } else {
            this.dataList = []
          }
        })
      },
      init (id) {
        this.material.materialId = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.material.materialId) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/material/info/${this.material.materialId}`),
              method: 'get',
              // params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.material.materialId = data.material.id,
                this.material.materialDesc = data.material.materialDesc,
                this.material.materialName = data.material.materialName,
                this.material.materialNum = data.material.materialNum,
                this.material.materialPrice = data.material.materialPrice,
                this.material.materialType = data.material.materialType,
                this.material.materialUnit = data.material.materialUnit
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/material/${!this.material.materialId ? 'saveMaterialAndSuppMaterial' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                // 'materialId':this.material.materialId,
                'materialNum':this.material.materialNum,
                'materialName':this.material.materialName,
                'materialType':this.material.materialType,
                'materialPrice':this.material.materialPrice,
                'materialUnit':this.material.materialUnit,
                'materialDesc':this.material.materialDesc,
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
